package com.kcloud.commons.tools.codebuilder;

import com.kcloud.commons.tools.codebuilder.impl.AuthorizationEntityCodeBuilder;
import com.kcloud.commons.tools.codebuilder.impl.JpRepositoryCodeBuilder;
import com.squareup.javapoet.JavaFile;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

/**
 * 代码生成器工具类
 * @author songhuiqing
 */
public class CodeBuilderFactory {

    private CodeBuilderConfig config;

    private CodeBuilderFactory(String folderPath, String packagePath, boolean ignorePrefix, String prefix) {
        config = new CodeBuilderConfig();
        config.setFolderPath(folderPath);
        config.setPackagePath(packagePath);
        config.setIgnorePrefix(ignorePrefix);
        config.setPrefix(prefix);
    }

    /**
     * 代码生成工厂类构造
     * @param folderPath 要生成的文件路径，不包含java代码的包路径部分，例如：/Users/songhuiqing/workspace/kcloud-commons-tools/src/main/java 或 D:\workspace\kcloud-commons-tools\src\main\java
     * @param packagePath 包路径，例如：com.kcloud.commons.tools.user
     * @param ignorePrefix 是否忽略表的前缀，true的话，生成的对象将不包含<code>prefix</code>前缀
     * @param prefix 表前缀
     * @return
     */
    public static CodeBuilderFactory newInstance(String folderPath, String packagePath, boolean ignorePrefix, String prefix) {

        return new CodeBuilderFactory(folderPath, packagePath, ignorePrefix, prefix);
    }
    public CodeBuilderFactory addDataSource(DataSource dataSource) {
        config.setDataSource(dataSource);
        return this;
    }

    public void generate(CodeCategory codeCategory, String[] tables) throws IOException {

        CodeBuilder codeBuilder = null;
        switch (codeCategory) {
            case ENTITY:
                codeBuilder = new AuthorizationEntityCodeBuilder(config);
                break;
            case REPOSITORY:
                codeBuilder = new JpRepositoryCodeBuilder(config);
        }
        List<JavaFile> fileList = codeBuilder.generate(tables);
        for (JavaFile f : fileList) {
            f.writeTo(Paths.get(config.getFolderPath()));
        }
    }

    private static DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://118.190.159.137:3306/demo?useSSL=false&serverTimezone=UTC");
        config.setUsername("root");
        config.setPassword("gang317");
        config.addDataSourceProperty("useInformationSchema", true);
        config.addDataSourceProperty("remarks", true);
        return new HikariDataSource(config);
    }

    public static void main(String[] args) throws IOException {
        String folderPath = "/Users/songhuiqing/workspace/idea/kcloud-commons/kcloud-commons-tools/src/main/java";
        String packagePath = "com.kcloud.commons.tools.codebuilder.user";
        CodeBuilderFactory.newInstance(folderPath, packagePath, true, "demo_")
                .addDataSource(dataSource())
                .generate(CodeCategory.ENTITY, new String[]{"demo_user", "demo_party_payment", "demo_work_experience"});
    }

}
